package demo02;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * synchronized -> Lock
 * wait -> condition.await
 * notifyAll -> condition.signalAll();
 */
class Aircondition {
    private int number = 0;
    private Lock lock = new ReentrantLock();
    private Condition condition = lock.newCondition();

    public void increment() throws Exception {
        lock.lock();
        try {
            // 1 判断
            while (number != 0) {
                condition.await();
            }

            // 2 干活
            number++;
            System.out.println(Thread.currentThread().getName() + "\t" + number);

            // 3 通知
            condition.signalAll();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void decrement() throws Exception {
        lock.lock();
        try {
            // 1 判断
            while (number == 0) {
                condition.await();
            }

            // 2 干活
            number--;
            System.out.println(Thread.currentThread().getName() + "\t" + number);

            // 3 通知
            condition.signalAll();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

//    public synchronized void increment() throws Exception {
//        // 1 判断
////        if (number != 0) {              # 这里一定要用while，官方文档wait有介绍，如果用if，多个线程会出错
//        while (number != 0) {
//            this.wait();
//        }
//
//        // 2 干活
//        number++;
//        System.out.println(Thread.currentThread().getName() + "\t" + number);
//
//        // 3 通知
//        this.notifyAll();
//    }
//
//    public synchronized void decrement() throws Exception {
//        // 1 判断
////        if (number == 0) {
//        while (number == 0) {
//            this.wait();
//        }
//
//        // 2 干活
//        number--;
//        System.out.println(Thread.currentThread().getName() + "\t" + number);
//
//        // 3 通知
//        this.notifyAll();
//    }
}


/**
 * 题目：现在两个线程，可以操作初始值为0的一个变量
 * 实现一个线程对变量加1，一个线程对变量减1，
 * 实现交替，来10轮，变量初始值为0
 *
 * 1 高内聚低耦合前提下，线程操作资源类
 * 判断/干活/通知
 */
public class ProdConsumerDemo04 {
    public static void main(String[] args) throws Exception{
        Aircondition aircondition = new Aircondition();

        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    aircondition.increment();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, "A").start();

        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    aircondition.decrement();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, "B").start();

        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    aircondition.increment();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, "C").start();

        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    aircondition.decrement();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, "D").start();
    }
}
